/**
 * Created by jackliu on 15/12/16.
 */

var common = require('../../common');
var pool = require('../index');
var mysql = require('mysql');
var ItbBaseModel = require('./ItbBaseModel');
var async  = require( 'async'); // 同步控制


// Schema 结构
var ItbFontModel = function(obj){
    ItbBaseModel.call(this,obj);

  // 以下为表定义数据，需要的字段就填入
        //`font_id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '字体ID',
        //`user_id` bigint(20) NOT NULL COMMENT '用户ID',
        //`user_type` int(1) NOT NULL DEFAULT '0' COMMENT '0后台; 1用户',
        //`font_name` varchar(100) NOT NULL DEFAULT '' COMMENT '字体sjg名称',
        //`offical_name` varchar(100) DEFAULT '' COMMENT '字体官方名称',
        //`alias` varchar(500) DEFAULT NULL COMMENT '字体别名(json数组): ["别名1","别名2","别名3"]',
        //`bold_italic` int(3) NOT NULL DEFAULT '0' COMMENT 'bold/italic等属性',
        //`file_name` varchar(100) NOT NULL DEFAULT '' COMMENT '字体原始文件名',
        //`url` varchar(500) NOT NULL DEFAULT '' COMMENT '公网存储地址',
        //`local_path` varchar(500) DEFAULT '' COMMENT '服务器本地存储路径',
        //`thumb_s` varchar(500) DEFAULT '' COMMENT '缩略图url',
        //`copyright` int(2) NOT NULL DEFAULT '0',
        //`publish` int(1) NOT NULL DEFAULT '0' COMMENT '0未发布,1对外发布',
        //`create_time` datetime NOT NULL,
        //`update_time` datetime NOT NULL,
        //`update_by` varchar(50) NOT NULL DEFAULT '',
        //`del_flg` int(1) NOT NULL DEFAULT '0',

};
ItbFontModel.prototype = new ItbBaseModel();

// 取得表名
ItbFontModel.prototype.getTableName = function(){
    return 'itb_font';
}
// 取得查询语句中where 后面匹配主键的条件（需要各派生类进行重载）
ItbFontModel.prototype.getWhereConditionOfKeyEqual = function(){
    return 'font_id='+this.font_id;
}

// 查找所有有效字体
// 参数：TODO
// 参数：callback 回调，参数如下：
//          error:错误
//          result:多行数据
ItbFontModel.prototype.findAllFont = function(callback){
    var THIS = this;
    var tableName = this.getTableName();

    pool.pool.getConnection(function(err, connection) {
        if (err) {
            console.log('ItbFontModel.findAllFont Err:'+err);
        }
        // Use the connection
        var sql = ' SELECT * FROM itbour.itb_font t WHERE t.del_flg = 0 and t.publish=1 ORDER BY t.font_id ';
        //var sqlValue = [];
        //var sql = mysql.format(sql, sqlValue);
        console.log(sql);
        connection.query(sql, function(error, result) {
            // And done with the connection.
            connection.release();

            console.log();

            callback(error, result);
        });
    });
}


// 导出
module.exports = ItbFontModel;